<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/1/24
 * Time: 17:15
 */

namespace app\index\logic;


use app\common\utils\Constant;
use think\Db;
use think\Exception;
use think\Log;

class UserLogic
{
    public function bindNote($open_id,$note_id){
        $user = Db::name('User')
            ->where(array('open_id'=>$open_id))
            ->find();
        Db::startTrans();
        try {
            if (!$user) {
                $user = $this->createUserByOpenId($open_id);
            }
            $map = array('note_id'=>$note_id,'user_id'=>$user['id']);
            $count = Db::name('UserNote')
                ->where($map)
                ->count();
            if($count == 0){
                $this->createNote($user['id'],$note_id);
            }
            Db::commit();
            $user_note_id = Db::name('UserNote')->where($map)->value('id');
            return array('status'=>1,'data'=>$user_note_id);
        }catch (Exception $e) {
            Db::rollback();
            Log::write('bindNote:'.$e->getMessage());
            return array('status' => 0, 'error' => $e->getMessage());
        }
    }

    public function bindNoteByUser($user_id,$note_id){
        $user = Db::name('User')
            ->where(array('id'=>$user_id))
            ->find();
        if(!$user){
            return array('status' => 2,'msg'=>'用户不存在！');
        }
        Db::startTrans();
        try {
            $map = array('note_id'=>$note_id,'user_id'=>$user['id']);
            $count = Db::name('UserNote')
                ->where($map)
                ->count();
            if($count == 0){
                $this->createNote($user['id'],$note_id);
            }
            Db::commit();
            $user_note_id = Db::name('UserNote')->where($map)->value('id');
            return array('status'=>1,'data'=>$user_note_id);
        }catch (Exception $e) {
            Db::rollback();
            Log::write('bindNote:'.$e->getMessage());
            return array('status' => 0, 'error' => $e->getMessage());
        }
    }

    private function createNote($user_id,$note_id){
        $temple_id = model('Note')->where(array('id'=>$note_id))->value('temple_id');
        $money = model('Temple')->where(array('id'=>$temple_id))->value('money');
        $user_note['user_id'] = $user_id;
        $user_note['note_id'] = $note_id;
        if($money == 0){
            $user_note['is_solve'] = 1;
        }else{
            $user_note['is_solve'] = 0;
        }
        $user_note['create_time'] = time();
        Db::name('UserNote')->insert($user_note);
    }

    private function createUserByOpenId($open_id){
        $user['createtime'] = time();
        $user['user_type'] = Constant::USER_TYPE_GENERAL;
        $user['username'] = substr($open_id,-7);
        $user['open_id'] = $open_id;
        $user = model('User')->create($user);
        return $user;
    }

    /**
     * 创建或更新用户信息
     * @param $open_id
     * @param $user_info
     * @return $this|array|false|\PDOStatement|string|\think\Model
     */
    public function saveOrUpdateUser($open_id,$user_info){
        $open_id = $open_id.'';
        $user = model('User')
            ->where(array('open_id'=>$open_id))
            ->find();
        if(!$user){
            //创建用户
            $user_info = $this->makeUser($user_info);
            $user_info['createtime'] = time();
            $user_info['open_id'] = $open_id;
            $user_info['user_type'] = Constant::USER_TYPE_GENERAL;
            $user = model('User')->create($user_info);
        }else{
            $user_info = $this->makeUser($user_info);
            $user_info['updatetime'] = time();
            $user_info['logintime'] = time();
            $user->save($user_info);
        }
        return $user;
    }

    public function createUser($open_id,$nickname){
        $open_id = $open_id.'';
        $user = model('User')
            ->where(array('open_id'=>$open_id))
            ->find();
        if(!$user){
            //创建用户
            $user_info['createtime'] = time();
            $user_info['open_id'] = $open_id;
            $user_info['user_type'] = Constant::USER_TYPE_GENERAL;
            $user_info['nickname'] = $nickname;
            $user = model('User')->create($user_info);
        }
        return $user;
    }

    private function makeUser($userInfo){
        $user = [];
        $userInfo = (array)$userInfo;
        if(isset($userInfo) && isset($userInfo['nickname'])) {
            $user['nickname'] = match_chinese($userInfo['nickname']);
            $user['avatar'] = $userInfo['headimgurl'];
            $sex = $userInfo['sex'] . '';
            $user['sex'] = $sex == '2' ? 1 : 0;
        }
        return $user;
    }
}